//
//  CellOfUserSearchResult.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/1.
//  Copyright © 2016年 KX. All rights reserved.
//
#import "FreeTravelMacro.h"
#import "FreeTravelNetwork.h"
#import "Masonry.h"
#import "CellOfUserSearchResult.h"
@interface CellOfUserSearchResult ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIImageView *imageView;
@end
@implementation CellOfUserSearchResult
- (instancetype)initWithFrame:(CGRect)frame{
    self= [super initWithFrame:frame];
    if (self) {
        [self createCollectionView];
        self.imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Unknown-1"]];
        [self.contentView addSubview:self.imageView];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(SCREEN_WIDTH/2);
        make.height.mas_offset(SCREEN_WIDTH/2);
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.centerX.equalTo(self.contentView.mas_centerX);
    }];
}
- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, 100);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    [self.collectionView registerClass:[CellOfDetailsPageUserSearchResult class] forCellWithReuseIdentifier:@"CellOfDetailsPageUserSearchResult"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.model.entry.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CellOfDetailsPageUserSearchResult *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellOfDetailsPageUserSearchResult" forIndexPath:indexPath];
    cell.model = self.model.entry[indexPath.item];
    return cell;
}

- (void)handleData{
    [FreeTravelNetwork GET:[NSString stringWithFormat:@"http://open.qyer.com/qyer/search/index?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&keyword=%@&page=1&place_field=country%%7Ccity%%7Cpoi&track_app_channel=App%%2520Store&track_app_version=7.0.1&track_device_info=iPhone%%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%%25209.3.4&type=user&v=1",self.mosaicString] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.model = [ModelOfUserDataSearchResult modelWithDic:[result objectForKey:@"data"]];
        if (self.model.entry.count == 0) {
            self.imageView.hidden = NO;
        }else{
            self.imageView.hidden = YES;
        }
        [self.collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
- (void)setModel:(ModelOfUserDataSearchResult *)model{
    _model = model;
    
}
- (void)setMosaicString:(NSString *)mosaicString{
    _mosaicString = mosaicString;
    [self handleData];
}
@end
